package com.heima.commons.utils;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

/**
 * HttpClient工具类
 *
 * @author wangmx
 */
public class HttpClientUtils {
    private static Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class);
    private static PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
    private static RequestConfig requestConfig;
    private static final int MAX_TOTAL = 100;
    private static final int MAX_TIMEOUT = 7000;
    private static final int CONNECT_TIMEOUT = 10000;
    private static final int SOCKET_TIMEOUT = 40000;
    private static final String CHARSET = "UTF-8";

    public HttpClientUtils() {
    }

    public static String doGet(String url) throws Exception {
        return doGet(url, new HashMap());
    }

    public static String doGet(String url, Map params) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doGet url is null or '' ");
            return result;
        } else {
            List pairList = new ArrayList(params.size());
            Iterator var4 = params.entrySet().iterator();

            while (var4.hasNext()) {
                Map.Entry entry = (Map.Entry) var4.next();
                NameValuePair pair = new BasicNameValuePair((String) entry.getKey(), entry.getValue().toString());
                pairList.add(pair);
            }

            CloseableHttpResponse response = null;
            InputStream instream = null;
            CloseableHttpClient httpclient = HttpClients.createDefault();

            try {
                URIBuilder URIBuilder = new URIBuilder(url);
                URIBuilder.addParameters(pairList);
                URI uri = URIBuilder.build();
                HttpGet httpGet = new HttpGet(uri);
                LOG.info("uri:{}", uri);
                response = httpclient.execute(httpGet);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doGet statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var16) {
                LOG.error("doGet  IO ERROR :{}", var16.getMessage());
            } catch (URISyntaxException var17) {
                LOG.error("doGet URISyntaxException :{}", var17.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpclient) {
                    httpclient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return result;
        }
    }
    /*
    这段代码实现了一个完整的HTTP GET请求流程，包括：
      参数校验。
      构建请求参数和URI。
      发送请求并处理响应。
      异常处理和资源释放。
      返回请求结果。*/
    //params.entrySet().iterator()的作用是获取一个迭代器，用于遍历Map中的键值对。这种方式是遍历Map的常用方法之一，适用于需要同时访问键和值的场景。

    //定义了一个静态方法doGet，用于发送HTTP GET请求。 这段代码是一个用于发送HTTP GET请求的Java方法，它通过Apache HttpClient库实现。
    //参数：String url：目标URL地址。  Map params：请求参数，键值对形式存储。  String charset：响应内容的字符编码。
    public static String doGet(String url, Map params, String charset) throws Exception {
        String result = null;
        //使用StringUtils.isEmpty方法（来自Apache Commons Lang库）来判断字符串是否为空
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doGet url is null or '' ");
            return result;
        } else {
            //创建一个ArrayList，大小初始化为params的大小
            List pairList = new ArrayList(params.size());
            //这行代码的作用是获取一个Map对象的条目集合（entrySet）的迭代器（Iterator），以便遍历Map中的键值对
            // (1) params.entrySet()
            //执行结果：[Map.Entry("key1", "value1"), Map.Entry("key2", "value2")]
            //作用：获取Map对象的条目集合（Set<Map.Entry<K, V>>）。params.entrySet()将Map中的键值对以Map.Entry的形式组织成一个集合。
            //解释：params是一个Map对象，Map是一个键值对的集合，其中键（key）是唯一的。entrySet()方法返回一个Set集合，集合中的每个元素都是一个Map.Entry对象，表示一个键值对。
            // (2) .iterator()
            //作用：获取条目集合的迭代器（Iterator）。
            //解释：
            //Iterator是Java中用于遍历集合的接口，提供了一种通用的方式来逐个访问集合中的元素。iterator()方法返回一个Iterator实例，用于遍历entrySet集合。
            //通过迭代器，可以逐个访问集合中的Map.Entry对象，从而获取每个键值对的键（key）和值（value）。
            // (3) Iterator var5
            //作用：声明一个Iterator类型的变量var5，并将其初始化为params.entrySet().iterator()的返回值。
            //解释：var5是一个Iterator对象，用于后续的遍历操作。在后续代码中，可以通过var5.hasNext()判断是否还有下一个元素，通过var5.next()获取下一个元素。
            Iterator var5 = params.entrySet().iterator();

            //遍历params的entrySet，将每个键值对转换为NameValuePair对象。将NameValuePair对象添加到pairList
            //作用：判断迭代器var5是否还有下一个元素。
            //解释：var5是一个Iterator对象，用于遍历params.entrySet()返回的条目集合。hasNext()方法返回一个布尔值，表示集合中是否还有未被访问的元素。
            while (var5.hasNext()) {
                //作用：获取迭代器var5的下一个元素，并将其强制转换为Map.Entry类型。
                //解释：var5.next()方法返回集合中的下一个元素，类型为Object。
                // 由于params.entrySet()返回的是Set<Map.Entry<K, V>>，因此需要将返回的Object强制转换为Map.Entry类型。
                // Map.Entry是一个接口，表示Map中的一个键值对，包含getKey()和getValue()方法。
                Map.Entry entry = (Map.Entry) var5.next();
                //作用：将Map.Entry对象转换为NameValuePair对象。
                //解释：
                //entry.getKey()获取当前键值对的键（key），并强制转换为String类型。entry.getValue().toString()获取当前键值对的值（value），并调用toString()方法将其转换为字符串。
                //使用BasicNameValuePair类的构造函数，将键和值封装为一个NameValuePair对象。NameValuePair是Apache HttpClient库中的一个接口，用于表示HTTP请求中的键值对参数
                NameValuePair pair = new BasicNameValuePair((String) entry.getKey(), entry.getValue().toString());
                pairList.add(pair);

                //执行这段代码后，pairList的内容将是：
                //   [
                //     NameValuePair("name", "Kimi"),
                //     NameValuePair("age", "25"),
                //     NameValuePair("isStudent", "false")
                //   ]
            }
            //创建一个默认的CloseableHttpClient实例，用于发送HTTP请求。
            //1. CloseableHttpResponse response = null;
            //作用：声明一个CloseableHttpResponse类型的变量response，并将其初始化为null。
            //解释：
            //CloseableHttpResponse是Apache HttpClient库中用于表示HTTP响应的类。它继承自HttpResponse，并实现了Closeable接口，支持资源关闭操作。
            //在发送HTTP请求后，response将用于存储从服务器返回的响应对象。初始化为null是为了在后续的代码中动态赋值，确保在发生异常时可以安全地关闭资源。
            //2. InputStream instream = null;
            //作用：声明一个InputStream类型的变量instream，并将其初始化为null。
            //解释：
            //InputStream是Java中用于读取二进制数据的输入流类。在处理HTTP响应时，响应体的内容通常是一个二进制流，可以通过HttpEntity.getContent()方法获取。
            //instream将用于存储从响应体中获取的输入流，以便后续读取响应内容。初始化为null是为了在后续的代码中动态赋值，同时确保在发生异常时可以安全地关闭流
            CloseableHttpResponse response = null;
            InputStream instream = null;
            //3. CloseableHttpClient httpclient = HttpClients.createDefault();
            //作用：创建一个CloseableHttpClient实例，并将其赋值给变量httpclient。
            //解释：
            //CloseableHttpClient是Apache HttpClient库中用于发送HTTP请求的客户端类。它继承自HttpClient，并实现了Closeable接口，支持资源关闭操作。
            //HttpClients.createDefault()是一个静态方法，用于创建一个默认配置的HTTP客户端实例。默认配置的客户端包括一些常用的设置，例如连接超时、默认的连接管理器等。这个客户端实例将用于发送HTTP请求（如HttpGet、HttpPost等）。
            CloseableHttpClient httpclient = HttpClients.createDefault();

            //作用：根据URL和请求参数构建完整的URI。
            //逻辑：使用URIBuilder类，以url为基础。调用addParameters(pairList)方法，将请求参数添加到URI中。调用build()方法生成最终的URI。
            try {
                URIBuilder URIBuilder = new URIBuilder(url);
                URIBuilder.addParameters(pairList);
                URI uri = URIBuilder.build();

                //作用：创建一个HttpGet对象，用于发送GET请求。
                //逻辑：使用构建好的URI实例初始化HttpGet对象。
                HttpGet httpGet = new HttpGet(uri);
                //作用：发送HTTP GET请求，并处理响应内容。
                //逻辑：
                //调用httpclient.execute(httpGet)发送请求，获取响应对象CloseableHttpResponse。获取响应状态码statusCode，并记录日志。从响应中获取HttpEntity对象。
                //如果HttpEntity不为null，调用entity.getContent()获取响应内容流InputStream。使用IOUtils.toString(instream, charset)将响应内容流转换为字符串，指定字符编码为charset
                response = httpclient.execute(httpGet);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doGet statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, charset);
                }
                //作用：捕获并处理可能发生的异常。
                //逻辑：捕获IOException，通常与网络问题或I/O操作有关。捕获URISyntaxException，通常与URI格式不正确有关。记录异常信息到日志
            } catch (IOException var17) {
                LOG.error("doGet  IO ERROR :{}", var17.getMessage());
            } catch (URISyntaxException var18) {
                LOG.error("doGet URISyntaxException :{}", var18.getMessage());
            } finally {
                //作用：确保所有资源在方法执行完成后被正确关闭，避免资源泄漏。
                //逻辑：在finally块中，依次检查instream、response和httpclient是否为null。如果不为null，调用它们的close()方法关闭资源。记录日志，确认资源已关闭
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpclient) {
                    httpclient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return result;
        }
    }

    public static String doPost(String apiUrl) throws Exception {
        return doPost(apiUrl, (Map) (new HashMap()));
    }

    public static String doPost(String url, Map params) throws Exception {
        String result = null;
        String param = "";
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doPost url is null or '' ");
            return result;
        } else {
            List pairList = new ArrayList(params.size());
            Iterator var5 = params.entrySet().iterator();

            while (var5.hasNext()) {
                Map.Entry entry = (Map.Entry) var5.next();
                NameValuePair pair = new BasicNameValuePair((String) entry.getKey(), entry.getValue().toString());
                pairList.add(pair);
                if (param.equals("")) {
                    param = (String) entry.getKey() + "=" + entry.getValue();
                } else {
                    param = param + "&" + (String) entry.getKey() + "=" + entry.getValue();
                }
            }

            LOG.info("http请求地址:" + url + "?" + param);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doPost statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                    LOG.info("doPost Result:{}", result);
                }
            } catch (IOException var14) {
                LOG.error("doPost  ERROR :{}", var14.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return result;
        }
    }

    public static String doPost(String url, String xml) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doPost url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                LOG.info("短信请求服务器地址:" + url + "?" + xml);
                httpPost.setConfig(requestConfig);
                httpPost.setEntity(new StringEntity(xml, "GBK"));
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doPost statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var12) {
                LOG.error("doPost  ERROR :{}", var12.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return result;
        }
    }

    public static String doPost(String url, Object json) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doPostByJson url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doPost statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var13) {
                LOG.error("doPost BY JSON ERROR :{}", var13.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

            }

            return result;
        }
    }

    public static String doPostPay(String url, Object json) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doPostByJson url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
                httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
                httpPost.setHeader("Accept", "application/json");
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doPost statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var13) {
                LOG.error("doPost BY JSON ERROR :{}", var13.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

            }

            return result;
        }
    }

    public static String doPostSSL(String apiUrl, Map params) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(apiUrl)) {
            LOG.info("warn:doPostSSL url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
            HttpPost httpPost = new HttpPost(apiUrl);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                List pairList = new ArrayList(params.size());
                Iterator var8 = params.entrySet().iterator();

                Map.Entry entry;
                while (var8.hasNext()) {
                    entry = (Map.Entry) var8.next();
                    NameValuePair pair = new BasicNameValuePair((String) entry.getKey(), entry.getValue().toString());
                    pairList.add(pair);
                }

                httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("utf-8")));
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 200) {
                    LOG.info("doPostSSL statusCode:{}", statusCode);
                    entry = null;
                    return String.valueOf(entry);
                }

                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (Exception var14) {
                LOG.error("doPostSSL ERROR :{}", var14.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return result;
        }
    }

    public static String doPostSSL(String apiUrl, Object json) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(apiUrl)) {
            LOG.info("warn:doPostSSL By Json url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
            HttpPost httpPost = new HttpPost(apiUrl);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            HttpEntity entity;
            try {
                httpPost.setConfig(requestConfig);
                StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    entity = response.getEntity();
                    if (entity != null) {
                        instream = entity.getContent();
                        result = IOUtils.toString(instream, "UTF-8");
                    }

                    return result;
                }

                LOG.info("doPostSSL by json statusCode:{}", statusCode);
                entity = null;
            } catch (Exception var13) {
                LOG.error("doPostSSL BY JSON ERROR :{}", var13.getMessage());
                return result;
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

                LOG.info("close  instream response httpClient  connection succ");
            }

            return String.valueOf(entity);
        }
    }

    private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
        SSLConnectionSocketFactory sslsf = null;

        try {
            SSLContext sslContext = (new SSLContextBuilder()).loadTrustMaterial((KeyStore) null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }

                public void verify(String host, SSLSocket ssl) throws IOException {
                }

                public void verify(String host, X509Certificate cert) throws SSLException {
                }

                public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                }
            });
        } catch (GeneralSecurityException var2) {
            LOG.error("createSSLConnSocketFactory ERROR :{}", var2.getMessage());
        }

        return sslsf;
    }

    public static String doPostPay(String url, Object json, String authorization) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("warn:doPostByJson url is null or '' ");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
                httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Authorization", authorization);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("doPost statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var14) {
                LOG.error("doPost BY JSON ERROR :{}", var14.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

            }

            return result;
        }
    }

    public static String doPostPayUpgraded(String url, Object json, String authorization) throws Exception {
        String result = null;
        if (StringUtils.isEmpty(url)) {
            LOG.info("新支付接口url不能为空！");
            return result;
        } else {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            InputStream instream = null;

            try {
                httpPost.setConfig(requestConfig);
                StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
                httpPost.setHeader("Authorization", authorization);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                LOG.info("新支付请求状态 statusCode:{}", statusCode);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    instream = entity.getContent();
                    result = IOUtils.toString(instream, "UTF-8");
                }
            } catch (IOException var14) {
                LOG.error("新支付接口发送异常:{}", var14.getMessage());
            } finally {
                if (null != instream) {
                    instream.close();
                }

                if (null != response) {
                    response.close();
                }

                if (null != httpClient) {
                    httpClient.close();
                }

            }

            return result;
        }
    }

    static {
        connMgr.setMaxTotal(100);
        connMgr.setDefaultMaxPerRoute(100);
        RequestConfig.Builder configBuilder = RequestConfig.custom();
        configBuilder.setConnectTimeout(10000);
        configBuilder.setSocketTimeout(40000);
        configBuilder.setConnectionRequestTimeout(7000);
        configBuilder.setStaleConnectionCheckEnabled(true);
        requestConfig = configBuilder.build();
    }
}
